package com.aspirecn.core.datacache.useragent;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;

import com.aspire.core.match.ua.object.DeviceUA;
import com.aspirecn.core.commons.Configuration;
import com.aspirecn.core.datacache.BaseLocalCache;
import com.csvreader.CsvReader;

/**
 * deviceua数据缓存
 * 
 * @author wangxinning
 * 
 */
public class DeviceUACache extends BaseLocalCache {

	public final static Logger logger = Logger.getLogger(DeviceUACache.class);

	// private Map<Integer, DeviceUA> deviceUAMap = new HashMap<Integer,
	// DeviceUA>();
	private List<DeviceUA> DeviceUAList = new ArrayList<DeviceUA>();

	private static DeviceUACache instance = new DeviceUACache();

	private DeviceUACache() {

		this.dataFile = new File(Configuration.getInstance()
				.getDownloadFilePath()
				+ File.separator
				+ "uaV3"
				+ File.separator + "deviceua.csv");
		load();
	}

	public static DeviceUACache getInstance() {
		return instance;
	}

	public void load() {

		if (!loading.getAndSet(true)
				&& Configuration.getInstance().switchUACache()
				&& this.dataFile.exists()) {

			this.clear();

			long start = System.currentTimeMillis();

			try {

				CsvReader reader = new CsvReader(this.dataFile.toString(), ',',
						Charset.forName("gbk"));

				while (reader.readRecord()) {

					if (reader.getCurrentRecord() == 0)// 设置列名
					{
						reader.setHeaders(reader.getValues());
					} else {
						DeviceUA ua = new DeviceUA();
						ua.setDevice_id(NumberUtils.toInt(reader
								.get("DEVICE_ID")));
						ua.setDevice_os_ua(StringUtils.remove(StringUtils
								.remove(reader.get("DEVICE_OS_UA"), "(null)"),
								"\n"));
						ua.setDevice_ua(StringUtils.remove(StringUtils.remove(
								reader.get("DEVICE_UA"), "(null)"), "\n"));
						ua.setDevice_ua_header(StringUtils.remove(StringUtils
								.remove(reader.get("DEVICE_UA_HEADER"),
										"(null)"), "\n"));

						DeviceUAList.add(ua);
					}
				}

				reader.close();

			} catch (Exception e) {
				logger.error("DeviceUA信息加载异常", e);
			}

			loading.set(false);
			long end = System.currentTimeMillis();
			updateFileDownloadTime();
			logger.info("DeviceUA信息加载完成，time=" + (end - start) + "ms,size="
					+ DeviceUAList.size());

		} else {
			loading.set(false);
			logger.info("配置关闭，DeviceUA信息未加载");
		}

		loading.set(false);
	}

	@Override
	public void clear() {

		DeviceUAList.clear();
	}

	@Override
	public void reload() {

		if (this.fileIsChange()) {
			this.load();
		} else {
			logger.info("没变化，DeviceUA信息未加载");
		}
	}

	public List<DeviceUA> getDeviceUA(int deviceid) {
		List<DeviceUA> retList = new ArrayList<DeviceUA>();
		for (DeviceUA ua : DeviceUAList) {
			if (ua.getDevice_id() == deviceid) {
				retList.add(ua);
			}
		}
		return retList;
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		DeviceUACache geoCache = DeviceUACache.getInstance();
		System.out.println(geoCache.getDeviceUA(7356));
		long end = System.currentTimeMillis();

		System.out.println(end - start);
	}
}